const {src,dest,watch,parallel} = require("gulp");

const babel = require("gulp-babel");

const minify=require("gulp-minify-css");

const uglify = require("gulp-uglify");

const rename = require("gulp-rename");

const concat = require("gulp-concat");

const sass = require("gulp-sass");

const connect = require("gulp-connect");


function copyFn(){        
    return src(["src/**/*"])
    .pipe(dest("dist"))
    .pipe(connect.reload())
}
exports.copy = copyFn;

function stfMinFn(){
    return src(["src/js/index.js","src/js/goodsCart.js","src/js/goodsDetails.js","src/js/goodsLists.js","src/js/login.js","src/js/register.js"])
    .pipe(babel({
        presets: ['@babel/env']
    }))
    .pipe(concat("vae.js"))
    .pipe(dest("dist/js"))
    .pipe(uglify())
    .pipe(rename("index.min.js"))
    .pipe(dest("dist/js"))
    .pipe(connect.reload())
}
exports.stfMin = stfMinFn;

function merCssFn(){
    return src(["src/css//goodsCart.css","src/css/goodsDetails.css","src/css/goodsLists.css","src/css/index.css","src/css/login.css","src/css/lrPublic.css","src/css/public.css","src/css/register.css","src/css/reset.css"])
    .pipe(concat("collette.css"))
    .pipe(dest("dist/css"))
    .pipe(minify("collette.min.css"))
    .pipe(dest("dist/css"));
}
exports.merCss=merCssFn;

function stcFn(){
    return src("src/sass/**/*.scss")
    .pipe(sass().on("error",sass.logError))
    .pipe(dest("dist/css"))
    .pipe(connect.reload())
}
exports.stc = stcFn;


function watchFn(cb){
    watch(["src/**/*","!src/js/**/*","!src/sass/**/*"],copyFn);
    watch("src/js/**/*",stfMinFn);
    watch("src/sass/**/*.scss",stcFn);
    cb();
}
exports.watch = watchFn;

function csFn(cb){
    connect.server({
        root:"dist",     //以哪个文件夹为服务器的跟目录
        port:3001,       //端口号
        livereload:true  //是否可以自动刷新
    })
    cb();
}
exports.cs = csFn;

exports.default = parallel(watchFn,csFn)



